package com.shchuangzhi.smartguard.core.receiver;

import android.graphics.Bitmap;
import android.view.View;
import android.widget.ImageView;
import com.shchuangzhi.smartguard.R;
import com.shchuangzhi.smartguard.core.Clock;
import com.shchuangzhi.smartguard.core.Consts;
import com.shchuangzhi.smartguard.core.ImageLoader;
import com.shchuangzhi.smartguard.core.NotifyMessage;
import com.shchuangzhi.smartguard.core.NotifyPrinter;
import com.shchuangzhi.smartguard.core.Util;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by j_hu on 11/1/17.
 */

public class ReceiverInOutData {
    private String ver;
    private String schoolNo;
    private String schoolName;
    private InOutStats stats;
    private long lastApplyTS = 0;
    private long lastApplyUnixTS = 0;
    private long refreshPeriod = 60;
    private List<InOutDataView> dataViewList = new ArrayList<>();
    private static final ReceiverInOutData inout = new ReceiverInOutData();

    public void apply(String ver, String schoolName, String schoolNo, List<SyncReceiverDataCommand.SyncData> list, InOutStats stats) {
        this.ver = ver;
        dataViewList.clear();
        for(SyncReceiverDataCommand.SyncData data : list) {
            dataViewList.add(new InOutDataView(
                    data.getNO(), schoolNo, data.getState(), data.getName(), data.getPhotoURL(), data.isMale()));
        }
        this.stats = stats;
        this.schoolNo = schoolNo;
        this.schoolName = schoolName;
        this.lastApplyUnixTS = Clock.getUnixTime();
        this.lastApplyTS = Clock.getCurrentInMillis();

        NotifyPrinter.getInstance().notify(NotifyMessage.wrap(null, Consts.NOTIFY_RECEIVER_STATS_CHANGED));
        NotifyPrinter.getInstance().notify(NotifyMessage.wrap(null, Consts.NOTIFY_RECEIVER_DATA_VIEW_CHANGEED));
    }

    public String getVer() {
        Calendar last = Calendar.getInstance();
        last.setTime(new Date(lastApplyTS));

        if (Clock.getCalendar().get(Calendar.HOUR) < last.get(Calendar.HOUR)) {
            if (stats != null) {
                int absenceCount = stats.getShouldAttendCount();
                for(Map.Entry<Integer, Integer> entry : stats.getValue().entrySet()) {
                    if (entry.getKey() == InOutStats.KEY_ABSENCE) {
                        stats.getValue().put(InOutStats.KEY_ABSENCE, absenceCount);
                    } else {
                        stats.getValue().put(entry.getKey(), 0);
                    }
                }
            }
            apply("0", schoolName, schoolNo, new ArrayList<SyncReceiverDataCommand.SyncData>(), stats);
        } else {
            if (Clock.getUnixTime() - lastApplyUnixTS > refreshPeriod) {
                ver = "0";
            }
        }

        return ver;
    }

    public String getSchoolName() {
        return schoolName;
    }

    public InOutStats getStats() {
        return stats;
    }

    public List<InOutDataView> getDataViewList() {
        return dataViewList;
    }

    public static final ReceiverInOutData getInstance() {
        return inout;
    }

    public class InOutDataView {
        private int no;
        private int state;
        private String name;
        private String photoURL;
        private String schoolNo;
        private boolean isMale;

        public InOutDataView(int no, String schoolNo, int state,  String name, String photoURL, boolean isMale) {
            this.no = no;
            this.state = state;
            this.name = name;
            this.photoURL = photoURL == null ? "" : photoURL;
            this.schoolNo = schoolNo;
            this.isMale = isMale;
        }

        public int getNo() {
            return no;
        }

        public String getSchoolNo() {
            return schoolNo;
        }

        public int getState() {
            return state;
        }

        public String getName() {
            return name;
        }

        public boolean isMale() {
            return isMale;
        }

        public boolean uploadEnabled() {
            return state != Consts.STATE_ABSENCE;
        }

        public void displayImage(ImageView photoView, ImageView stateView) {
            if (state != Consts.STATE_ABSENCE) {
                if (photoURL.length() != 0) {
                    ImageLoader.getInstance().displayImage(photoURL, photoView, new ImageLoader.ImageLoadingCompleteHandler(){
                        @Override
                        public void onComplete(String imageUri, View view, Bitmap loadedImage) {
                            if (state == Consts.STATE_OUT) {
                                ImageView imgView = (ImageView)view;
                                imgView.setImageBitmap(Util.convertGreyImg(loadedImage));
                            }
                        }
                    });
                } else {
                    photoView.setImageResource(isMale ? R.drawable.boy : R.drawable.girl);
                }
            }

            switch (state) {
                case Consts.STATE_WATCH:stateView.setImageResource(R.drawable.watch);
                break;
                case Consts.STATE_SICK:stateView.setImageResource(R.drawable.sick);
                break;
                case Consts.STATE_OUT:stateView.setImageResource(R.drawable.bye);
                break;
                case Consts.STATE_ABSENCE:
                    photoView.setImageResource(R.drawable.absence);
                    stateView.setImageDrawable(null);
                break;
                default:stateView.setImageResource(R.drawable.health);
                break;
            }
        }
    }

    public class InOutStats {
        private int classNo;
        private String className;
        private Map<Integer, Integer> value;

        private static final int KEY_TOTAL = 99;
        private static final int KEY_HEALTH = 0;
        private static final int KEY_WATCH = 2;
        private static final int KEY_SICK = 3;
        private static final int KEY_ABSENCE = -1;
        private static final int KEY_BYE = 1;

        public InOutStats(int classNo, String className, Map<Integer, Integer> value) {
            this.classNo = classNo;
            this.className = className;
            this.value = value;
        }

        public int getClassNo() {
            return classNo;
        }
        public String getClassName() {
            return className;
        }
        public Map<Integer, Integer> getValue() {
            return value;
        }
        public Integer getHealthCount() {
            return get(KEY_HEALTH);
        }
        public Integer getSickCount() {
            return get(KEY_SICK);
        }
        public Integer getWatchCount() {
            return get(KEY_WATCH);
        }
        public Integer getByeCount() {
            return get(KEY_BYE);
        }
        public Integer getAbsenceCount() {
            return get(KEY_ABSENCE);
        }
        public Integer getShouldAttendCount() {
            return get(KEY_TOTAL) - get(KEY_BYE);
        }

        private Integer get(int key) {
            int count = 0;
            if (value.containsKey(key)) {
                count = value.get(key);
            }
            return count;
        }
    }
}
